package com.jtjrenren.android.taxi.passenger.core.wallect.coupons;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.jtjrenren.android.taxi.passenger.core.adapter.CouponsListAdapter;
import com.jtjrenren.android.taxi.passenger.base.BaseActivity;
import com.jtjrenren.android.taxi.passenger.Constants;
import com.jtjrenren.android.taxi.passenger.R;
import com.jtjrenren.android.taxi.passenger.bean.User;
import com.jtjrenren.android.taxi.passenger.bean.api.ApiCouList;
import com.jtjrenren.android.taxi.passenger.manger.AppManger;
import com.jtjrenren.android.taxi.passenger.manger.api.ApiWorks;
import com.jtjrenren.android.taxi.passenger.utils.InitView;
import com.library.weidget.AutoLoadRecyclerView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by wdl on 2016/1/27.
 */
public class CouponsActivity extends BaseActivity {
    @BindView(R.id.coupons_list)
    AutoLoadRecyclerView rcy_list;
    @BindView(R.id.nocontent)
    LinearLayout ll_nocontent;
    @BindView(R.id.title)
    TextView tv_title;

    private int coupons_type = Constants.DEFAULT_COUPONS_LIST_ALL;
    private boolean isChoose = false; //是否选择
    private ApiCouList.DataListBean selectCoupons;

    private User user;
    private CouponsListAdapter adapter;

    public static Intent getDiyIntent(Activity context,int type,boolean isChoose,ApiCouList.DataListBean coupons){
        Intent intent=new Intent(context,CouponsActivity.class);
        intent.putExtra("type",type);
        intent.putExtra("choose",isChoose);
        if (coupons!=null)
            intent.putExtra("coupon",coupons);
        return intent;
    }


    @Override
    public int getLayoutId() {
        return R.layout.aty_wallect_coupons;
    }

    @Override
    public void init() {
        tv_title.setText("打车券");
        user = AppManger.getInstance().user;

        if (getIntent().hasExtra("type")) {
            coupons_type = getIntent().getIntExtra("type", Constants.DEFAULT_COUPONS_LIST_ALL);
        }
        if (getIntent().hasExtra("choose")) {
            isChoose = getIntent().getBooleanExtra("choose", false);
        }
        if (getIntent().hasExtra("coupon")) {
            selectCoupons = (ApiCouList.DataListBean) getIntent().getSerializableExtra("coupon");
        }


        InitView.initVritalReclyView(this, rcy_list);

        adapter = new CouponsListAdapter(this, isChoose);
        rcy_list.setAdapter(adapter);
        if (isChoose && selectCoupons != null) {
            adapter.setSelectedCoupons(selectCoupons);
        }
        adapter.setItemClickListener(new CouponsListAdapter.ItemClickListener() {
            @Override
            public void itemClick(boolean isHeader) {
                selectCoupons = adapter.getSelectedCoupons();
                Intent data = new Intent();
                data.putExtra("data", selectCoupons);
                setResult(RESULT_OK, data);
                finish();
            }
        });

        getCouponsList();
    }

    @OnClick(R.id.title_left)
    public void clickBack(){
        finish();
    }

    private void getCouponsList() {
        showWaittingDialog(null, true);
        ApiWorks.getCouponList(user.getWorkNo(), coupons_type, new ApiWorks.ResponseListener<ApiCouList>() {
            @Override
            public void onResponse(ApiCouList apiCouList) {
                dismissWaittingDialog();

                if (apiCouList != null && apiCouList.getDataList() != null && apiCouList.getDataList().size() > 0) {
                    ll_nocontent.setVisibility(View.GONE);
                    rcy_list.setVisibility(View.VISIBLE);
                    adapter.replace(apiCouList.getDataList());
                } else {
                    ll_nocontent.setVisibility(View.VISIBLE);
                    rcy_list.setVisibility(View.GONE);
                }
            }
        });
    }
}
